package com.time.company.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * XRefresh Recycler Adapter 的基类
 * Created by Bing on 2016/8/23.
 * E-mail is tx16bing@163.com
 */
public abstract class BaseXRecAdapter<T> extends BaseRecyclerAdapter<BaseViewHolder> {



    protected static int TYPE_COUNTER = 1;
    protected Context mContext;
    protected LayoutInflater inflater;
    public OnItemClickListener onItemClickListener;
    public OnItemLongClickListener onItemLongClickListener;

    // 数据源
    protected List<T> mList;
    protected int viewType;

    public BaseXRecAdapter(Context mContext, List<T> list, int viewTypeCount) {
        this.mContext = mContext;
        this.inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setData(list);
        TYPE_COUNTER = viewTypeCount;
    }

    private void setData(List<T> list) {
        if (list == null){
            this.mList = new ArrayList<T>();
        }else{
            this.mList = list;
        }
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }

    public int getViewType() {
        return viewType;
    }


    public void updateData(List<T> list){
        setData(list);
        notifyDataSetChanged();
    }

    @Override
    public int getAdapterItemCount() {
        return this.mList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public T getItem(int position){
        return this.mList.get(position);
    }

    @Override
    public BaseViewHolder getViewHolder(View view) {
        return new BaseViewHolder(view);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface OnItemClickListener<T>{
        void onItemClick(View view, int position, T t, int type);
    }

    public interface OnItemLongClickListener<T>{
        void onItemLongClick(View view, int position, T t, int type);
    }

}
